//
//  WJTopic.m
//  BeDeJie
//
//  Created by jerry on 2017/2/1.
//  Copyright © 2017年 jerry. All rights reserved.
//

#import "WJTopic.h"

@implementation WJTopic


- (CGFloat)cellHeight{
    // 如果已经计算过，就直接返回
    if (_cellHeight) return _cellHeight;

    CGFloat cellHeight = 0;
    
    // 增加顶部的高度
    cellHeight += 60; //60包含头像的高度和头你的上边距和下边距
    
    //文字的高度
    CGFloat sizeMaxWidth = screenWidth - 2*WJMarin; //文字可显示的最大宽度
    CGSize textMaxSize = CGSizeMake(sizeMaxWidth, MAXFLOAT);
    CGFloat textHeight = [self.text sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:textMaxSize].height;
    //增加文字的高度
    cellHeight += textHeight;
    
    // 中间的内容
    if (self.type != WJTopicTypeWord) { // 中间有内容（图片、声音、视频）
        /*
         self.width     middleW
         ----------- == -------
         self.height    middleH
         
         self.width * middleH == middleW * self.height
         */
        
        CGFloat middleW = textMaxSize.width;
        CGFloat middleH = middleW * self.height / self.width;
        if (middleH >= screenHeight) { // 显示的图片高度超过一个屏幕，就是超长图片
            middleH = 300;
            _bigPicture = YES;
        }
        CGFloat middleY = cellHeight + WJMarin;  //WJMarin用于与上面的文字保持间隔
        CGFloat middleX = WJMarin;
        self.middleFrame = CGRectMake(middleX, middleY, middleW, middleH);
        cellHeight += middleH + WJMarin;
    }
    
    
    // 最热评论
    if (self.top_cmt.count) { // 有最热评论
        // 标题
        cellHeight += 21;   //21是标题的高度，可通过xib查看
        
        // 内容
        NSDictionary *cmt = self.top_cmt.firstObject;
        NSString *content = cmt[@"content"];
        if (content.length == 0) {
            content = @"[语音评论]";
        }
        NSString *username = cmt[@"user"][@"username"];
        NSString *cmtText = [NSString stringWithFormat:@"%@ : %@", username, content];
        CGFloat cmtTextHeight = [cmtText boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16]} context:nil].size.height;
        cellHeight += cmtTextHeight + WJMarin;  //评论上下各有５边距
    }

    //增加底部的高度（包含上边距）
    cellHeight += 35 + WJMarin;  //35代表底部控件的高度
    
    return cellHeight;
}


@end
